#!/usr/bin/env python
#-*- coding:utf-8 -*-
from datetime import datetime

#数据转化辅助类
class conv(object):

    #数据转为int，转换失败默认为0
    @staticmethod
    def toInt(data):
        if data=='' or data==None:
            return 0
        try:
            return int(data)
        except Exception as e:
            return 0
    
    #数据转为可为空的int
    @staticmethod
    def toIntOrNull(data):
        if data=='' or data==None:
            return None
        try:
            return int(data)
        except Exception as e:
            return None

    #数据转为float
    @staticmethod
    def toFloat(data):
        if data=='' or data==None:
            return 0
        try:
            return float(data)
        except Exception as e:
            return 0

    #数据转为可为空的float 
    @staticmethod
    def toFloatOrNull(data):
        if data=='' or data==None:
            return None
        try:
            return float(data)
        except Exception as e:
            return None
    
    #数据转化为日期
    @staticmethod 
    def toDate(data):
        if data=='' or data==None:
            return datetime.strptime('1970-01-01','%Y-%m-%d')
        try:
            return datetime.strptime(data,'%Y-%m-%d %H:%M:%S')
        except Exception as e:
            return datetime.strptime('1970-01-01','%Y-%m-%d')
    
    #数据转化为可为空的日期
    @staticmethod
    def toDateOrNull(data):
        if data=='' or data==None:
            return None
        try:
            return datetime.strptime(data,'%Y-%m-%d %H:%M:%S')
        except Exception as e:
            return None


